package com.ruoyi.controller.middlesign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rfp.ws.utils.ECCFileUtils;
import com.rfp.ws.utils.ECCUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 中征平台公有方法
 * @date 2021/3/15 16:28
 */
public class MiddlesMethod {

    /**
     * 获取head
     *
     * @return
     */
    public static String getHead(String TRANSCODE, String code) {

        String pubKey = getPubKey(code);

        String Head = "<TRANSCODE>" + TRANSCODE + "</TRANSCODE>" +
                "<MSGID>" + MiddlesUtils.getUUid() + "</MSGID>" +
                "<ORGNLMSGID>" + code + "</ORGNLMSGID>" +
                "<SESSIONNO>aaa</SESSIONNO>" +
                "<SENDDATE>" + MiddlesUtils.getYearMonthDay() + "</SENDDATE>" +
                "<SENDTIME>" + MiddlesUtils.getTime() + "</SENDTIME>" +
                "<ERRCODE></ERRCODE>" +
                "<ERRMSG></ERRMSG>";
        try {
            String headEncryptStr = ECCUtils.encrypt(Head, pubKey);
            return headEncryptStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * B2801接口 核心企业查询授权
     *
     * @return
     */
    public static String B2801(String code) {
        String pubKey = getPubKey(code);

        String Body = "<ORG_NO>" + code + "</ORG_NO>" +
                "<CD_NEW></CD_NEW>";
        try {
            String bodyEncryptStr = ECCUtils.encrypt(Body, pubKey);
            return bodyEncryptStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * B2803接口 核心企业查询授信
     *
     * @return
     */
    public static String B2803(String code) {

        String pubKey = getPubKey(code);

        String Body = "<ORG_NO>" + code + "</ORG_NO>" +
                "<CD_NEW></CD_NEW>";
        try {
            String bodyEncryptStr = ECCUtils.encrypt(Body, pubKey);
            return bodyEncryptStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 拼装发送数据
     *
     * @param head
     * @param body
     * @return
     */
    public static String getSendData(String head, String body, String code) {
        String data = "<DOCUMENT>"
                + "<TITLE>" + code + "</TITLE>"
                + "<HEAD>" + head + "</HEAD>"
                + "<BODY>" + body + "</BODY>"
                + "</DOCUMENT>";
        return data;
    }

    /**
     * 解密返回报文
     *
     * @return
     */
    public static String decryptData(String responseMsg, String code) {
        String priKey = getPriKey(code);
        try {
            //解密返回报文的body节点
            String head2 = responseMsg.substring(responseMsg.lastIndexOf("<HEAD>") + "<HEAD>".length(), responseMsg.lastIndexOf("</HEAD>"));
            String body2 = responseMsg.substring(responseMsg.lastIndexOf("<BODY>") + "<BODY>".length(), responseMsg.lastIndexOf("</BODY>"));

            String decryptData = "<?XML version=\"1.0\" encoding=\"UTF-8\"?><DOCUMENT>"
                    + "<TITLE>" + code + "</TITLE>"
                    + "<HEAD>" + ECCUtils.decrypt(head2, priKey) + "</HEAD>"
                    + "<BODY>" + ECCUtils.decrypt(body2, priKey) + "</BODY>"
                    + "</DOCUMENT>";
            return decryptData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 发送B2802数据
     *
     * @param path
     * @param code
     * @return
     */
    public static String sendB2802(String path, String code) {
        String filePath = path;
        String head = MiddlesMethod.getHead("B2802", code);
        String body = "<ORG_NO>" + code + "</ORG_NO><CD_NEW>1</CD_NEW>";
        return callHttpService(filePath, head, body, code);
    }

    /**
     * 发送B2804数据
     *
     * @param path
     * @param code
     * @return
     */
    public static String sendB2804(String path, String code) {
        String filePath = path;
        String head = MiddlesMethod.getHead("B2804", code);
        String body = "<ORG_NO>" + code + "</ORG_NO><CD_NEW>1</CD_NEW>";
        return callHttpService(filePath, head, body, code);
    }

    /**
     * 发送B2805数据
     *
     * @param path
     * @param code
     * @return
     */
    public static String sendB2805(String path, String code) {
        String filePath = path;
        String head = MiddlesMethod.getHead("B2805", code);
        String body = "<ORG_NO>" + code + "</ORG_NO><CD_NEW>1</CD_NEW>";
        return callHttpService(filePath, head, body, code);
    }

    /**
     * 发送B2806数据
     *
     * @param path
     * @param code
     * @return
     */
    public static String sendB2806(String path, String code) {
        String filePath = path;
        String head = MiddlesMethod.getHead("B2806", code);
        String body = "<ORG_NO>" + code + "</ORG_NO><CD_NEW>1</CD_NEW>";
        return callHttpService(filePath, head, body, code);
    }

    /**
     * 发送B2807数据
     *
     * @param path
     * @param code
     * @return
     */
    public static String sendB2807(String path, String code) {
        String filePath = path;
        String head = MiddlesMethod.getHead("B2807", code);
        String body = "<ORG_NO>" + code + "</ORG_NO><CD_NEW>1</CD_NEW>";
        return callHttpService(filePath, head, body, code);
    }

    public static String callHttpService(String filePath, String head, String body, String code) {
        String pubKey = getPubKey(code);
        try {
            String bodyEncryptStr = ECCUtils.encrypt(body, pubKey);
            String data = "<DOCUMENT>"
                    + "<TITLE>" + code + "</TITLE>"
                    + "<HEAD>" + head + "</HEAD>"
                    + "<BODY>" + bodyEncryptStr + "</BODY>"
                    + "</DOCUMENT>";
            String file = ECCFileUtils.encryptFile(filePath, pubKey);
            String responseMsg = MiddlesUtils.callHttpService(MiddlesConfig.uri, "serviceAndFile", data, file);
            return MiddlesMethod.decryptData(responseMsg, code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取B2801授权数据
     *
     * @param hxqyCode
     * @return
     */
    public static List<Map<String, Object>> getAuthorizationList(String hxqyCode, String hxqyName) {
        //调用B2801接口获取到授权信息
        String head = MiddlesMethod.getHead("B2801", hxqyCode);
        String body = MiddlesMethod.B2801(hxqyCode);
        String sendData = MiddlesMethod.getSendData(head, body, hxqyCode);
        //这个List用来存储B2801返回的授权信息集合
        List<Map<String, Object>> maps = new ArrayList<>();
        try {
            //发送请求并返回报文
            String responseMsg = MiddlesUtils.callHttpService(MiddlesConfig.uri, "serviceAndFile", sendData, "");
            //解密报文
            String xmlStr = MiddlesMethod.decryptData(responseMsg, hxqyCode);
            xmlStr = xmlStr.substring(38, xmlStr.length());
            //String xmlStr = "<DOCUMENT><TITLE>913411007049623209</TITLE><HEAD><MSGID>913411007049623209202101161610779552349</MSGID><ORGNLMSGID>2987599a582947a8b4183328a36c5bf8</ORGNLMSGID><TRANSCODE>B2801</TRANSCODE><SESSIONNO></SESSIONNO><SENDDATE>20210116</SENDDATE><SENDTIME>144552</SENDTIME><ERRCODE>000000</ERRCODE><ERRMSG>成功</ERRMSG></HEAD><BODY><LIST><AUTH_INFO><CREDIT_NAME>蚌埠市城创建筑装饰工程有限公司</CREDIT_NAME><CREDIT_CODE>91341500MA2U0T7T9X</CREDIT_CODE><CREDIT_NO>AU2021020300019838</CREDIT_NO><HIS_START_DATE>20210106</HIS_START_DATE><HIS_END_DATE>20200101</HIS_END_DATE><MARK1></MARK1><MARK2></MARK2><MARK3></MARK3></AUTH_INFO></LIST></BODY></DOCUMENT>";
            //如果解密后的xml能找到CREDIT_NAME,说明有授权信息
            if (xmlStr.indexOf("CREDIT_NAME") != -1) {
                Document doc = DocumentHelper.parseText(xmlStr);
                JSONObject json = new JSONObject();
                MiddlesUtils.dom4j2Json(doc.getRootElement(), json);
                Map map1 = (Map) JSON.parse(json.toJSONString());
                Map map2 = (Map) map1.get("BODY");
                Map map3 = (Map) map2.get("LIST");
                try {
                    Map map4 = (Map) map3.get("AUTH_INFO");
                    maps = new ArrayList<>();
                    maps.add(map4);
                } catch (Exception e) {
                    maps = (List) map3.get("AUTH_INFO");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Map map : maps) {
            map.put("hxqyName", hxqyName);
            map.put("hxqyCode", hxqyCode);
        }
        return maps;
    }


    public static List<Map<String, Object>> getCreditList(String hxqyCode, String hxqyName) {
        //调用B2803接口获取到授信息
        /*
        String head = MiddlesMethod.getHead("B2803", hxqyCode);
        String body = MiddlesMethod.B2803(hxqyCode);
        String sendData = MiddlesMethod.getSendData(head, body, hxqyCode);
        //这个List用来存储B2803返回的授权信信集合
        List<Map<String, Object>> maps = new ArrayList<>();
        try {
            //发送请求并返回报文
            String responseMsg = MiddlesUtils.callHttpService(MiddlesConfig.uri, "serviceAndFile", sendData, "");
            //解密报文并打印
            String xmlStr = MiddlesMethod.decryptData(responseMsg, hxqyCode);
            //String xmlStr = "<?XML version=\"1.0\" encoding=\"UTF-8\"?><DOCUMENT><TITLE>913411007049623209</TITLE><HEAD><MSGID>913411007049623209202103041614823617772</MSGID><ORGNLMSGID>689ef338700d4307adfaa5730328df60</ORGNLMSGID><TRANSCODE>B2803</TRANSCODE><SESSIONNO></SESSIONNO><SENDDATE>20210304</SENDDATE><SENDTIME>100657</SENDTIME><ERRCODE>000000</ERRCODE><ERRMSG>成功</ERRMSG></HEAD><BODY><LIST><CREDIT_INFO><CREDIT_CODE>15511401040008734</CREDIT_CODE><CREDIT_NAME>蚌埠市城创建筑装饰工程有限公司</CREDIT_NAME><LIMIT_AMOUNT>999999999999.99</LIMIT_AMOUNT><LIMIT_CURCD>CNY</LIMIT_CURCD><LIMIT_LOOP>1</LIMIT_LOOP><LIMIT_DATE>20210331</LIMIT_DATE><CREDIT_RATING>1</CREDIT_RATING><BANK_CODE>ahjpjszjf</BANK_CODE><BANK_NAME>安徽金鹏建设资金方</BANK_NAME><MARK1></MARK1><MARK2></MARK2><MARK3></MARK3><MARK4></MARK4><MARK5></MARK5></CREDIT_INFO></LIST></BODY></DOCUMENT>";
            MiddlesUtils.Additional("D:\\中征平台文件\\报文.txt", "B0283解密报文");
            MiddlesUtils.Additional("D:\\中征平台文件\\报文.txt", xmlStr);
            xmlStr = xmlStr.substring(38, xmlStr.length());
            if (xmlStr.indexOf("CREDIT_NAME") != -1) {
                Document doc = DocumentHelper.parseText(xmlStr);
                JSONObject json = new JSONObject();
                MiddlesUtils.dom4j2Json(doc.getRootElement(), json);
                Map map1 = (Map) JSON.parse(json.toJSONString());
                Map map2 = (Map) map1.get("BODY");
                Map map3 = (Map) map2.get("LIST");
                try {
                    Map map4 = (Map) map3.get("CREDIT_INFO");
                    maps = new ArrayList<>();
                    maps.add(map4);
                } catch (Exception e) {
                    maps = (List) map3.get("CREDIT_INFO");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Map map : maps) {
            map.put("hxqyName", hxqyName);
            map.put("hxqyCode", hxqyCode);
        }
*/

        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String,Object> map = new HashMap();
        map.put("CREDIT_NAME","滁州市友西混凝土有限公司全椒分公司");
        map.put("CREDIT_CODE","91341124MA2U6J352L");
        map.put("hxqyName","安徽金鹏建设集团股份有限公司");
        map.put("hxqyCode","913411007049623209");

        Map<String,Object> map1 = new HashMap();
        map1.put("CREDIT_NAME","滁州市万鑫商贸有限公司");
        map1.put("CREDIT_CODE","91341102MA2MX2ER67");
        map1.put("hxqyName","安徽金鹏建设集团股份有限公司");
        map1.put("hxqyCode","913411007049623209");
        mapList.add(map);
        mapList.add(map1);
        return mapList;
    }

    public static String getPubKey(String code){
        if(code.equals("913411007049623209")){
            return MiddlesConfig.pubKey1;
        }else if(code.equals("91341103MA2WEWU717")){

        }
        return "";
    }

    public static String getPriKey(String code){
        if(code.equals("913411007049623209")){
            return MiddlesConfig.priKey2;
        }else if(code.equals("91341103MA2WEWU717")){

        }
        return "";
    }

}
